<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>
<div class="blog-content-box">
    <div class="article-header-box">
        <div class="article-header">
            <div class="article-title-box">
                <h1 class="title-article" id="articleContentId">7.Python基础语法（五）【循环语句】</h1>
            </div>
            <div class="article-info-box">
                <div class="article-bar-top">
                    <img class="article-type-img" src="https://csdnimg.cn/release/blogv2/dist/pc/img/original.png" alt="">
                    <div class="bar-content">
                      <a class="follow-nickName " href="https://gu-han-zhe.blog.csdn.net" target="_blank" rel="noopener" title="孤寒者">孤寒者</a>
                    <img class="article-time-img article-heard-img" src="https://csdnimg.cn/release/blogv2/dist/pc/img/newUpTime2.png" alt="">
                    <span class="time">已于&nbsp;2022-02-04 23:05:45&nbsp;修改</span>
                   <div class="read-count-box">
                      <img class="article-read-img article-heard-img" src="https://csdnimg.cn/release/blogv2/dist/pc/img/articleReadEyes2.png" alt="">
                      <span class="read-count">阅读量2.6w</span>
                      <a id="blog_detail_zk_collection" class="un-collection" data-report-click="{&quot;mod&quot;:&quot;popu_823&quot;,&quot;spm&quot;:&quot;1001.2101.3001.4232&quot;,&quot;ab&quot;:&quot;new&quot;}">
                          <img class="article-collect-img article-heard-img un-collect-status isdefault" style="display:inline-block" src="https://csdnimg.cn/release/blogv2/dist/pc/img/tobarCollect2.png" alt="">
                          <img class="article-collect-img article-heard-img collect-status isactive" style="display:none" src="https://csdnimg.cn/release/blogv2/dist/pc/img/tobarCollectionActive2.png" alt="">
                          <span class="name">收藏</span>
                          <span class="get-collection" style="color: rgb(153, 154, 170);">
                              13
                          </span>
                      </a>
                      <div class="read-count-box is-like">
                        <img class="article-read-img article-heard-img" style="display:none" id="is-like-imgactive-new" src="https://csdnimg.cn/release/blogv2/dist/pc/img/newHeart2023Active.png" alt="">
                        <img class="article-read-img article-heard-img" style="display:block" id="is-like-img-new" src="https://csdnimg.cn/release/blogv2/dist/pc/img/newHeart2023Black.png" alt="">
                        <span class="read-count" id="blog-digg-num">点赞数
                            20
                        </span>
                      </div>
                    </div>
                  </div>
                </div>
                <div class="blog-tags-box">
                    <div class="tags-box artic-tag-box">
                            <span class="label">分类专栏：</span>
                                <a class="tag-link" href="https://blog.csdn.net/qq_44907926/category_11476279.html" target="_blank" rel="noopener">Python全栈系列教程</a>
                            <span class="label">文章标签：</span>
                                <a rel="nofollow" data-report-query="spm=1001.2101.3001.4223" data-report-click="{&quot;mod&quot;:&quot;popu_626&quot;,&quot;spm&quot;:&quot;1001.2101.3001.4223&quot;,&quot;strategy&quot;:&quot;python&quot;,&quot;ab&quot;:&quot;new&quot;,&quot;extra&quot;:&quot;{\&quot;searchword\&quot;:\&quot;python\&quot;}&quot;}" class="tag-link" href="https://so.csdn.net/so/search/s.do?q=python&amp;t=all&amp;o=vip&amp;s=&amp;l=&amp;f=&amp;viparticle=&amp;from_tracking_code=tag_word&amp;from_code=app_blog_art" target="_blank">python</a>
                                <a rel="nofollow" data-report-query="spm=1001.2101.3001.4223" data-report-click="{&quot;mod&quot;:&quot;popu_626&quot;,&quot;spm&quot;:&quot;1001.2101.3001.4223&quot;,&quot;strategy&quot;:&quot;开发语言&quot;,&quot;ab&quot;:&quot;new&quot;,&quot;extra&quot;:&quot;{\&quot;searchword\&quot;:\&quot;开发语言\&quot;}&quot;}" class="tag-link" href="https://so.csdn.net/so/search/s.do?q=%E5%BC%80%E5%8F%91%E8%AF%AD%E8%A8%80&amp;t=all&amp;o=vip&amp;s=&amp;l=&amp;f=&amp;viparticle=&amp;from_tracking_code=tag_word&amp;from_code=app_blog_art" target="_blank">开发语言</a>
                                <a rel="nofollow" data-report-query="spm=1001.2101.3001.4223" data-report-click="{&quot;mod&quot;:&quot;popu_626&quot;,&quot;spm&quot;:&quot;1001.2101.3001.4223&quot;,&quot;strategy&quot;:&quot;后端&quot;,&quot;ab&quot;:&quot;new&quot;,&quot;extra&quot;:&quot;{\&quot;searchword\&quot;:\&quot;后端\&quot;}&quot;}" class="tag-link" href="https://so.csdn.net/so/search/s.do?q=%E5%90%8E%E7%AB%AF&amp;t=all&amp;o=vip&amp;s=&amp;l=&amp;f=&amp;viparticle=&amp;from_tracking_code=tag_word&amp;from_code=app_blog_art" target="_blank">后端</a>
                                <a rel="nofollow" data-report-query="spm=1001.2101.3001.4223" data-report-click="{&quot;mod&quot;:&quot;popu_626&quot;,&quot;spm&quot;:&quot;1001.2101.3001.4223&quot;,&quot;strategy&quot;:&quot;for&quot;,&quot;ab&quot;:&quot;new&quot;,&quot;extra&quot;:&quot;{\&quot;searchword\&quot;:\&quot;for\&quot;}&quot;}" class="tag-link" href="https://so.csdn.net/so/search/s.do?q=for&amp;t=all&amp;o=vip&amp;s=&amp;l=&amp;f=&amp;viparticle=&amp;from_tracking_code=tag_word&amp;from_code=app_blog_art" target="_blank">for</a>
                                <a rel="nofollow" data-report-query="spm=1001.2101.3001.4223" data-report-click="{&quot;mod&quot;:&quot;popu_626&quot;,&quot;spm&quot;:&quot;1001.2101.3001.4223&quot;,&quot;strategy&quot;:&quot;while&quot;,&quot;ab&quot;:&quot;new&quot;,&quot;extra&quot;:&quot;{\&quot;searchword\&quot;:\&quot;while\&quot;}&quot;}" class="tag-link" href="https://so.csdn.net/so/search/s.do?q=while&amp;t=all&amp;o=vip&amp;s=&amp;l=&amp;f=&amp;viparticle=&amp;from_tracking_code=tag_word&amp;from_code=app_blog_art" target="_blank">while</a>
                    </div>
                </div>
                <div class="up-time"><span>于&nbsp;2022-02-04 20:49:52&nbsp;首次发布</span></div>
                <div class="slide-content-box">
                    <div class="article-copyright">
                        <div class="creativecommons">
                            转载需文末联系本作者（未经本人允许的转载完全不允许！）
                        </div>
                        <div class="article-source-link">
                            本文链接：<a href="https://blog.csdn.net/qq_44907926/article/details/122692383" target="_blank">https://blog.csdn.net/qq_44907926/article/details/122692383</a>
                        </div>
                    </div>
                </div>

                <div class="operating">
                    <a class="href-article-edit slide-toggle">版权</a>
                </div>
            </div>
        </div>

    </div>

        <div id="blogColumnPayAdvert">
            <div class="column-group">
                <div class="column-group-item column-group0 column-group-item-one">
                    <div class="item-l">
                        <a class="item-target" href="https://blog.csdn.net/qq_44907926/category_11476279.html" target="_blank" title="Python全栈系列教程" data-report-click="{&quot;spm&quot;:&quot;1001.2101.3001.6332&quot;}">
                            <img class="item-target" src="https://i-blog.csdnimg.cn/direct/48b1ca23c6944a049566077f75775d99.jpeg?x-oss-process=image/resize,m_fixed,h_224,w_224" alt="">
                            <span class="title item-target">
                                <span>
                                <span class="tit">Python全栈系列教程</span>
                                    <span class="dec">专栏收录该内容</span>
                                </span>
                            </span>
                        </a>
                    </div>
                    <div class="item-m">
                        <span>173 篇文章</span>
                        <span>461 订阅</span>
                        <span class="old-add-new-box">
                            <span class="price">¥99.90</span>
                            <span class="oldprice">¥299.90</span>
                        </span>
                    </div>
                    <div class="item-r">
                                <a class="item-target article-column-subscribe">已订阅</a>
                    </div>
                </div>
            </div>
        </div>
      <div class="ai-abstract-box">
         <div class="ai-abstract">
          <div class="abstract-content">
            <img class="lock-img" src="https://img-home.csdnimg.cn/images/20240711042549.png" alt="">
            本文详细介绍了Python的for和while循环语句，包括while循环的continue和break命令，for循环的使用，以及else语句的应用。还提供了实战示例，如continue和break的使用场景，以及计算质数等。
          </div>
          <span>摘要由CSDN通过智能技术生成</span>
        </div>
      </div>
    <article class="baidu_pl">
        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-704d5b9767.css">

                <div id="content_views" class="markdown_views prism-tomorrow-night">
                    <svg xmlns="http://www.w3.org/2000/svg" style="display: none;">
                        <path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path>
                    </svg>
                    <p></p>
<div class="toc">
 <h4><a name="t0"></a>目录：</h4>
 <ul><li><a href="#Python_2" rel="nofollow" target="_self">Python基础语法（五）</a></li><li><ul><li><a href="#14_Python_4" rel="nofollow" target="_self">14. Python循环语句</a></li><li><ul><li><a href="#141_while_21" rel="nofollow" target="_self">14.1 while循环</a></li><li><ul><li><a href="#continuebreak_42" rel="nofollow" target="_self">①重要命令：continue和break</a></li><li><a href="#while_else__81" rel="nofollow" target="_self">②while循环使用 else 语句</a></li><li><a href="#pass_99" rel="nofollow" target="_self">③pass语句</a></li></ul>
    </li><li><a href="#142_for_112" rel="nofollow" target="_self">14.2 for循环</a></li><li><ul><li><a href="#_133" rel="nofollow" target="_self">①上实操——通过索引序列迭代元素</a></li><li><a href="#for_else__150" rel="nofollow" target="_self">②for循环使用 else 语句</a></li></ul>
    </li><li><a href="#143_continue_185" rel="nofollow" target="_self">14.3 拓展两个有关于continue的例子</a></li></ul>
  </li></ul>
 </li></ul>
</div>
<p></p>
<h2><a name="t1"></a><a id="Python_2"></a><a href="https://so.csdn.net/so/search?q=Python%E5%9F%BA%E7%A1%80%E8%AF%AD%E6%B3%95&amp;spm=1001.2101.3001.7020" target="_blank" class="hl hl-1" data-report-click="{&quot;spm&quot;:&quot;1001.2101.3001.7020&quot;,&quot;dest&quot;:&quot;https://so.csdn.net/so/search?q=Python%E5%9F%BA%E7%A1%80%E8%AF%AD%E6%B3%95&amp;spm=1001.2101.3001.7020&quot;,&quot;extra&quot;:&quot;{\&quot;searchword\&quot;:\&quot;Python基础语法\&quot;}&quot;}" data-tit="Python基础语法" data-pretit="python基础语法">Python基础语法</a>（五）</h2>
<h3><a name="t2"></a><a id="14_Python_4"></a>14. <a href="https://so.csdn.net/so/search?q=Python%E5%BE%AA%E7%8E%AF%E8%AF%AD%E5%8F%A5&amp;spm=1001.2101.3001.7020" target="_blank" class="hl hl-1" data-report-click="{&quot;spm&quot;:&quot;1001.2101.3001.7020&quot;,&quot;dest&quot;:&quot;https://so.csdn.net/so/search?q=Python%E5%BE%AA%E7%8E%AF%E8%AF%AD%E5%8F%A5&amp;spm=1001.2101.3001.7020&quot;,&quot;extra&quot;:&quot;{\&quot;searchword\&quot;:\&quot;Python循环语句\&quot;}&quot;}" data-tit="Python循环语句" data-pretit="python循环语句">Python循环语句</a></h3>
<p><strong>python中的循环语句有for和while两种，下面带领大家一同深入研究一下：</strong></p>
<ol><li>Python for 循环语法：</li></ol>
<pre data-index="0" class="set-code-show prettyprint"><code class="prism language-python has-numbering" onclick="mdcp.copyCode(event)" style="position: unset;"><span class="token keyword">for</span> iterating_var <span class="token keyword">in</span> sequence<span class="token punctuation">:</span>
   statements<span class="token punctuation">(</span>s<span class="token punctuation">)</span>
<div class="hljs-button {2}" data-title="复制"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li></ul></pre>
<ol start="2"><li>Python while 循环语法：</li></ol>
<pre data-index="1" class="set-code-show prettyprint"><code class="prism language-python has-numbering" onclick="mdcp.copyCode(event)" style="position: unset;"><span class="token keyword">while</span> expression<span class="token punctuation">:</span>
   statement<span class="token punctuation">(</span>s<span class="token punctuation">)</span>
<div class="hljs-button {2}" data-title="复制"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li></ul></pre>
<ul><li>上述只是最基本的for和while语法，你可以看成是一个组件，你可以灵活的嵌套使用，可以在循环体内嵌入其他的循环体，如在while循环中可以嵌入for循环， 反之，你可以在for循环中嵌入while循环。</li></ul>
<h4><a name="t3"></a><a id="141_while_21"></a>14.1 while循环</h4>
<ul><li>执行流程图如下：<br> <img src="https://i-blog.csdnimg.cn/blog_migrate/70d5fb52546452c70be25db8c4944cd8.png" alt="在这里插入图片描述"><br> while循环就是不停的判断条件是否成立，条件成立则执行循环体，条件不成立时不执行循环体，退出循环</li></ul>
<p>上实操：</p>
<pre data-index="2" class="set-code-show prettyprint"><code class="prism language-python has-numbering" onclick="mdcp.copyCode(event)" style="position: unset;"><span class="token comment"># -*- coding: utf-8 -*-</span>
<span class="token comment"># __author__ = 小小明-代码实体</span>

count <span class="token operator">=</span> <span class="token number">0</span>
<span class="token keyword">while</span> <span class="token punctuation">(</span>count <span class="token operator">&lt;</span> <span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'The count is:'</span><span class="token punctuation">,</span> count<span class="token punctuation">)</span>
    count <span class="token operator">=</span> count <span class="token operator">+</span> <span class="token number">1</span>

<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"Good bye!"</span><span class="token punctuation">)</span>
<div class="hljs-button {2} mddef" data-title="复制" data-report-click="{&quot;spm&quot;:&quot;3001.10243&quot;}"></div></code><ul class="pre-numbering" style="opacity: 0.41358;"><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li></ul>'<div class="code-edithtml active " data-title="运行"><div data-report-view="{&quot;spm&quot;:&quot;3001.10182&quot;,&quot;extra&quot;:{&quot;index&quot;:0,&quot;ab&quot;:&quot;exp1&quot;}}" data-report-click="{&quot;spm&quot;:&quot;3001.10182&quot;,&quot;extra&quot;:{&quot;index&quot;:0,&quot;ab&quot;:&quot;exp1&quot;}}" class="code-edithtml-box code-edithtml-box0"><i></i>运行<div><button>运行</button></div></div></div></pre>
<p><img src="https://i-blog.csdnimg.cn/blog_migrate/07cdb66b28cc881468dab6db27f5785b.png" alt="在这里插入图片描述"></p>
<h5><a id="continuebreak_42"></a>①重要命令：continue和break</h5>
<p>while 语句时还有另外两个重要的命令 continue，break 来跳过循环，<strong>continue 用于跳过该次循环，break 则是用于跳过整个循环（即退出循环）</strong>，此外"判断条件"还可以是个常值，表示循环必定成立。</p>
<p><font color="red">第一个：break 语句</font><br>   Python break语句，就像在C语言中，打破了最小封闭for或while循环。<br>   break语句用来终止循环语句，即循环条件没有False或者序列还没被完全<a href="https://edu.csdn.net/course/detail/40020?utm_source=glcblog&amp;spm=1001.2101.3001.7020" target="_blank" class="hl hl-1" data-report-view="{&quot;spm&quot;:&quot;1001.2101.3001.7020&quot;,&quot;dest&quot;:&quot;https://edu.csdn.net/course/detail/40020?utm_source=glcblog&amp;spm=1001.2101.3001.7020&quot;,&quot;extra&quot;:&quot;{\&quot;word\&quot;:\&quot;递归\&quot;}&quot;}" data-report-click="{&quot;spm&quot;:&quot;1001.2101.3001.7020&quot;,&quot;dest&quot;:&quot;https://edu.csdn.net/course/detail/40020?utm_source=glcblog&amp;spm=1001.2101.3001.7020&quot;,&quot;extra&quot;:&quot;{\&quot;word\&quot;:\&quot;递归\&quot;}&quot;}" data-tit="递归" data-pretit="递归">递归</a>完，也会停止执行循环语句。<br>   break语句用在while和for循环中。<br>   如果您使用嵌套循环，break语句将停止执行最深层的循环，并开始执行下一行代码。</p>
<p><font color="red">第二个：continue 语句</font><br>   Python continue 语句跳出本次循环，而break跳出整个循环，这俩一定要区分清哦！<br>   continue 语句用来告诉Python跳过当前循环的剩余语句，然后继续进行下一轮循环。<br>   continue语句用在while和for循环中。</p>
<p>二者具体用法如下：</p>
<pre data-index="3" class="set-code-hide prettyprint"><code class="prism language-python has-numbering" onclick="mdcp.copyCode(event)" style="position: unset;"><span class="token comment"># -*- coding: utf-8 -*-</span>
<span class="token comment"># _author__ = 小小明-代码实体</span>

i <span class="token operator">=</span> <span class="token number">1</span>
<span class="token keyword">while</span> i <span class="token operator">&lt;</span> <span class="token number">5</span><span class="token punctuation">:</span>
    i <span class="token operator">+=</span> <span class="token number">1</span>
    <span class="token keyword">if</span> i <span class="token operator">%</span> <span class="token number">2</span> <span class="token operator">&gt;</span> <span class="token number">0</span><span class="token punctuation">:</span>  <span class="token comment"># 非双数时跳过输出</span>
        <span class="token keyword">continue</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span>  <span class="token comment"># 输出双数2、4</span>

i <span class="token operator">=</span> <span class="token number">1</span>
<span class="token keyword">while</span> <span class="token number">1</span><span class="token punctuation">:</span>  <span class="token comment"># 循环条件为1必定成立</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span>  <span class="token comment"># 输出1~10</span>
    i <span class="token operator">+=</span> <span class="token number">1</span>
    <span class="token keyword">if</span> i <span class="token operator">&gt;</span> <span class="token number">5</span><span class="token punctuation">:</span>  <span class="token comment"># 当i大于5时跳出循环</span>
        <span class="token keyword">break</span>
<div class="hljs-button {2} mddef" data-title="复制" data-report-click="{&quot;spm&quot;:&quot;3001.10243&quot;}"></div></code><div class="hide-preCode-box"><span class="hide-preCode-bt" data-report-view="{&quot;spm&quot;:&quot;1001.2101.3001.7365&quot;}"><img class="look-more-preCode contentImg-no-view" src="https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png" alt="" title=""></span></div><ul class="pre-numbering" style="opacity: 0.41358;"><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li><li style="color: rgb(153, 153, 153);">16</li></ul>'<div class="code-edithtml active " data-title="运行"><div data-report-view="{&quot;spm&quot;:&quot;3001.10182&quot;,&quot;extra&quot;:{&quot;index&quot;:1,&quot;ab&quot;:&quot;exp1&quot;}}" data-report-click="{&quot;spm&quot;:&quot;3001.10182&quot;,&quot;extra&quot;:{&quot;index&quot;:1,&quot;ab&quot;:&quot;exp1&quot;}}" class="code-edithtml-box code-edithtml-box1"><i></i>运行<div><button>运行</button></div></div></div></pre>
<p><img src="https://i-blog.csdnimg.cn/blog_migrate/0941a8c2e8c85d44ee9f238acbe13d13.png" alt="在这里插入图片描述"></p>
<h5><a id="while_else__81"></a>②while循环使用 else 语句</h5>
<ul><li>else的作用是判断循环是否正常结束，如果循环是被break中断else里的代码块就不再执行</li></ul>
<p>上实操：</p>
<pre data-index="4" class="set-code-show prettyprint"><code class="prism language-python has-numbering" onclick="mdcp.copyCode(event)" style="position: unset;"><span class="token comment"># -*- coding: utf-8 -*-</span>
<span class="token comment"># _author__ = 小小明-代码实体</span>

count <span class="token operator">=</span> <span class="token number">0</span>
<span class="token keyword">while</span> count <span class="token operator">&lt;</span> <span class="token number">5</span><span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span>count<span class="token punctuation">,</span> <span class="token string">" is  less than 5"</span><span class="token punctuation">)</span>
    count <span class="token operator">=</span> count <span class="token operator">+</span> <span class="token number">1</span>
<span class="token keyword">else</span><span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span>count<span class="token punctuation">,</span> <span class="token string">" is not less than 5"</span><span class="token punctuation">)</span>
<div class="hljs-button {2} mddef" data-title="复制" data-report-click="{&quot;spm&quot;:&quot;3001.10243&quot;}"></div></code><ul class="pre-numbering" style="opacity: 0.41358;"><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li></ul>'<div class="code-edithtml active " data-title="运行"><div data-report-view="{&quot;spm&quot;:&quot;3001.10182&quot;,&quot;extra&quot;:{&quot;index&quot;:2,&quot;ab&quot;:&quot;exp1&quot;}}" data-report-click="{&quot;spm&quot;:&quot;3001.10182&quot;,&quot;extra&quot;:{&quot;index&quot;:2,&quot;ab&quot;:&quot;exp1&quot;}}" class="code-edithtml-box code-edithtml-box2"><i></i>运行<div><button>运行</button></div></div></div></pre>
<p><img src="https://i-blog.csdnimg.cn/blog_migrate/1c25d426ee0bc431f7e9c77917bd4b4a.png" alt="在这里插入图片描述"></p>
<h5><a id="pass_99"></a>③pass语句</h5>
<ul><li>pass的作用，表示占位符。</li></ul>
<pre data-index="5" class="set-code-show prettyprint"><code class="prism language-python has-numbering" onclick="mdcp.copyCode(event)" style="position: unset;"><span class="token keyword">while</span> <span class="token boolean">True</span><span class="token punctuation">:</span>
    <span class="token keyword">pass</span>
<div class="hljs-button {2}" data-title="复制"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li></ul></pre>
<p>上面这个程序是不动的，但是不报错。<br> 占位符pass的作用：</p>
<ol><li>语句的完整性就相当于一个大家庭，pass的目的是保证语义的完整性。</li><li>保证格式的一个完整性</li><li>就是一个空语句</li></ol>
<h4><a name="t4"></a><a id="142_for_112"></a>14.2 for循环</h4>
<ul><li>执行流程图如下：<br> <img src="https://i-blog.csdnimg.cn/blog_migrate/c9ea03abde57ce6d3dff7007fda8545a.png" alt="在这里插入图片描述"><br> for循环可以遍历任何<strong>序列</strong>的项目，如一个列表或者一个字符串</li></ul>
<p>上实操：</p>
<pre data-index="6" class="set-code-show prettyprint"><code class="prism language-python has-numbering" onclick="mdcp.copyCode(event)" style="position: unset;"><span class="token comment"># -*- coding: utf-8 -*-</span>
<span class="token comment"># _author__ = 小小明-代码实体</span>

<span class="token keyword">for</span> letter <span class="token keyword">in</span> <span class="token string">'Python'</span><span class="token punctuation">:</span>  <span class="token comment"># 第一个实例</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'当前字母 :'</span><span class="token punctuation">,</span> letter<span class="token punctuation">)</span>

fruits <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'banana'</span><span class="token punctuation">,</span> <span class="token string">'apple'</span><span class="token punctuation">,</span> <span class="token string">'mango'</span><span class="token punctuation">]</span>
<span class="token keyword">for</span> fruit <span class="token keyword">in</span> fruits<span class="token punctuation">:</span>  <span class="token comment"># 第二个实例</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'当前水果 :'</span><span class="token punctuation">,</span> fruit<span class="token punctuation">)</span>

<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"Good bye!"</span><span class="token punctuation">)</span>
<div class="hljs-button {2} mddef" data-title="复制" data-report-click="{&quot;spm&quot;:&quot;3001.10243&quot;}"></div></code><ul class="pre-numbering" style="opacity: 0.41358;"><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li></ul>'<div class="code-edithtml active " data-title="运行"><div data-report-view="{&quot;spm&quot;:&quot;3001.10182&quot;,&quot;extra&quot;:{&quot;index&quot;:3,&quot;ab&quot;:&quot;exp1&quot;}}" data-report-click="{&quot;spm&quot;:&quot;3001.10182&quot;,&quot;extra&quot;:{&quot;index&quot;:3,&quot;ab&quot;:&quot;exp1&quot;}}" class="code-edithtml-box code-edithtml-box3"><i></i>运行<div><button>运行</button></div></div></div></pre>
<p><img src="https://i-blog.csdnimg.cn/blog_migrate/8191406cb71e5f761d1702c173c17e40.png" alt="在这里插入图片描述"></p>
<h5><a id="_133"></a>①上实操——通过索引序列迭代元素</h5>
<p>（大家可以先形象理解下，后面学完列表再回过头来看就会感觉豁然开朗了哦~）</p>
<pre data-index="7" class="set-code-show prettyprint"><code class="prism language-python has-numbering" onclick="mdcp.copyCode(event)" style="position: unset;"><span class="token comment"># -*- coding: utf-8 -*-</span>
<span class="token comment"># _author__ = 小小明-代码实体</span>

fruits <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'banana'</span><span class="token punctuation">,</span> <span class="token string">'apple'</span><span class="token punctuation">,</span> <span class="token string">'mango'</span><span class="token punctuation">]</span>
<span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token builtin">len</span><span class="token punctuation">(</span>fruits<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'当前水果 :'</span><span class="token punctuation">,</span> fruits<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"Good bye!"</span><span class="token punctuation">)</span>
<div class="hljs-button {2} mddef" data-title="复制" data-report-click="{&quot;spm&quot;:&quot;3001.10243&quot;}"></div></code><ul class="pre-numbering" style="opacity: 0.41358;"><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li></ul>'<div class="code-edithtml active " data-title="运行"><div data-report-view="{&quot;spm&quot;:&quot;3001.10182&quot;,&quot;extra&quot;:{&quot;index&quot;:4,&quot;ab&quot;:&quot;exp1&quot;}}" data-report-click="{&quot;spm&quot;:&quot;3001.10182&quot;,&quot;extra&quot;:{&quot;index&quot;:4,&quot;ab&quot;:&quot;exp1&quot;}}" class="code-edithtml-box code-edithtml-box4"><i></i>运行<div><button>运行</button></div></div></div></pre>
<p><img src="https://i-blog.csdnimg.cn/blog_migrate/44e9f97887fbc5ced37801945115db3e.png" alt="在这里插入图片描述"></p>
<p><strong>小知识点：</strong></p>
<ul><li>range是范围，从0开始，取头不取尾！</li></ul>
<h5><a id="for_else__150"></a>②for循环使用 else 语句</h5>
<p>  在 python 中，for … else ,for 中的语句和普通的没有区别，else 中的语句会在循环正常执行完（即 for 不是通过 break 跳出而中断的）的情况下执行，while … else 也是一样。<br>   也就是说for循环中的else部分是循环结束后执行的，但如果循环被break结束后，else部分便不再执行。</p>
<p>实操一：</p>
<pre data-index="8" class="set-code-show prettyprint"><code class="prism language-python has-numbering" onclick="mdcp.copyCode(event)" style="position: unset;"><span class="token comment"># -*- coding: utf-8 -*-</span>
<span class="token comment"># _author__ = 小小明-代码实体</span>

<span class="token keyword">for</span> num <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">20</span><span class="token punctuation">)</span><span class="token punctuation">:</span>    <span class="token comment"># 迭代 10 到 20 之间的数字</span>
    <span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> num<span class="token punctuation">)</span><span class="token punctuation">:</span>  <span class="token comment"># 根据因子迭代</span>
        <span class="token keyword">if</span> num <span class="token operator">%</span> i <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">:</span>     <span class="token comment"># 确定第一个因子</span>
            j <span class="token operator">=</span> num <span class="token operator">/</span> i      <span class="token comment"># 计算第二个因子</span>
            <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'%d 等于 %d * %d'</span> <span class="token operator">%</span> <span class="token punctuation">(</span>num<span class="token punctuation">,</span> i<span class="token punctuation">,</span> j<span class="token punctuation">)</span><span class="token punctuation">)</span>
            <span class="token keyword">break</span>            <span class="token comment"># 跳出当前循环</span>
    <span class="token keyword">else</span><span class="token punctuation">:</span>                    <span class="token comment"># 循环的 else 部分</span>
        <span class="token keyword">print</span><span class="token punctuation">(</span>num<span class="token punctuation">,</span> <span class="token string">'是一个质数'</span><span class="token punctuation">)</span>
<div class="hljs-button {2} mddef" data-title="复制" data-report-click="{&quot;spm&quot;:&quot;3001.10243&quot;}"></div></code><ul class="pre-numbering" style="opacity: 0.41358;"><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li></ul>'<div class="code-edithtml active " data-title="运行"><div data-report-view="{&quot;spm&quot;:&quot;3001.10182&quot;,&quot;extra&quot;:{&quot;index&quot;:5,&quot;ab&quot;:&quot;exp1&quot;}}" data-report-click="{&quot;spm&quot;:&quot;3001.10182&quot;,&quot;extra&quot;:{&quot;index&quot;:5,&quot;ab&quot;:&quot;exp1&quot;}}" class="code-edithtml-box code-edithtml-box5"><i></i>运行<div><button>运行</button></div></div></div></pre>
<p><img src="https://i-blog.csdnimg.cn/blog_migrate/be7c7037c15beb1bfd087f8088f77027.png" alt="在这里插入图片描述"><br> 实操二——计算出100以内的质数：</p>
<pre data-index="9" class="set-code-show prettyprint"><code class="prism language-python has-numbering" onclick="mdcp.copyCode(event)" style="position: unset;"><span class="token comment"># -*- coding: utf-8 -*-</span>
<span class="token comment"># _author__ = 小小明-代码实体</span>

prime <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
<span class="token keyword">for</span> num <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">:</span>    <span class="token comment"># 迭代 2 到 100 之间的数字</span>
    <span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> num<span class="token punctuation">)</span><span class="token punctuation">:</span>  <span class="token comment"># 根据因子迭代</span>
        <span class="token keyword">if</span> num <span class="token operator">%</span> i <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">:</span>     <span class="token comment"># 确定第一个因子</span>
            <span class="token keyword">break</span>            <span class="token comment"># 跳出当前循环</span>
    <span class="token keyword">else</span><span class="token punctuation">:</span>  <span class="token comment"># 循环的 else 部分</span>
        prime<span class="token punctuation">.</span>append<span class="token punctuation">(</span>num<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>prime<span class="token punctuation">)</span>
<div class="hljs-button {2} mddef" data-title="复制" data-report-click="{&quot;spm&quot;:&quot;3001.10243&quot;}"></div></code><ul class="pre-numbering" style="opacity: 0.41358;"><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li></ul>'<div class="code-edithtml active " data-title="运行"><div data-report-view="{&quot;spm&quot;:&quot;3001.10182&quot;,&quot;extra&quot;:{&quot;index&quot;:6,&quot;ab&quot;:&quot;exp1&quot;}}" data-report-click="{&quot;spm&quot;:&quot;3001.10182&quot;,&quot;extra&quot;:{&quot;index&quot;:6,&quot;ab&quot;:&quot;exp1&quot;}}" class="code-edithtml-box code-edithtml-box6"><i></i>运行<div><button>运行</button></div></div></div></pre>
<p><img src="https://i-blog.csdnimg.cn/blog_migrate/7b9e85ac947344202bee078b4a4fc3c0.png" alt="在这里插入图片描述"></p>
<h4><a name="t5"></a><a id="143_continue_185"></a>14.3 拓展两个有关于continue的例子</h4>
<pre data-index="10" class="set-code-show prettyprint"><code class="prism language-python has-numbering" onclick="mdcp.copyCode(event)" style="position: unset;"><span class="token comment"># -*- coding: utf-8 -*-</span>
<span class="token comment"># _author__ = 小小明-代码实体</span>

<span class="token keyword">for</span> letter <span class="token keyword">in</span> <span class="token string">'Python'</span><span class="token punctuation">:</span>
    <span class="token keyword">if</span> letter <span class="token operator">==</span> <span class="token string">'h'</span><span class="token punctuation">:</span>
        <span class="token keyword">continue</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'当前字母 :'</span><span class="token punctuation">,</span> letter<span class="token punctuation">)</span>

var <span class="token operator">=</span> <span class="token number">5</span>
<span class="token keyword">while</span> var <span class="token operator">&gt;</span> <span class="token number">0</span><span class="token punctuation">:</span>
    var <span class="token operator">=</span> var <span class="token operator">-</span> <span class="token number">1</span>
    <span class="token keyword">if</span> var <span class="token operator">==</span> <span class="token number">2</span><span class="token punctuation">:</span>
        <span class="token keyword">continue</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'当前变量值 :'</span><span class="token punctuation">,</span> var<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"Good bye!"</span><span class="token punctuation">)</span>
<div class="hljs-button {2} mddef" data-title="复制" data-report-click="{&quot;spm&quot;:&quot;3001.10243&quot;}"></div></code><ul class="pre-numbering" style="opacity: 0.41358;"><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li></ul>'<div class="code-edithtml active " data-title="运行"><div data-report-view="{&quot;spm&quot;:&quot;3001.10182&quot;,&quot;extra&quot;:{&quot;index&quot;:7,&quot;ab&quot;:&quot;exp1&quot;}}" data-report-click="{&quot;spm&quot;:&quot;3001.10182&quot;,&quot;extra&quot;:{&quot;index&quot;:7,&quot;ab&quot;:&quot;exp1&quot;}}" class="code-edithtml-box code-edithtml-box7"><i></i>运行<div><button>运行</button></div></div></div></pre>
<p><img src="https://i-blog.csdnimg.cn/blog_migrate/3821df959ad3803214cf097c2f6c969f.png" alt="在这里插入图片描述"></p>
                </div><div data-report-view="{&quot;mod&quot;:&quot;1585297308_001&quot;,&quot;spm&quot;:&quot;1001.2101.3001.6548&quot;,&quot;dest&quot;:&quot;https://gu-han-zhe.blog.csdn.net/article/details/122692383&quot;,&quot;extend1&quot;:&quot;pc&quot;,&quot;ab&quot;:&quot;new&quot;}"><div></div></div>
                <link href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/markdown_views-f23dff6052.css" rel="stylesheet">
                <link href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/style-e504d6a974.css" rel="stylesheet">
        </div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"><div class="blog_extension blog_extension_type1" id="blog_extension">
          <div class="blog_extension_card" data-report-click="{&quot;spm&quot;:&quot;1001.2101.3001.6470&quot;}" data-report-view="{&quot;spm&quot;:&quot;1001.2101.3001.6470&quot;}">
            <div class="blog_extension_card_left">
            <img src="https://img-blog.csdnimg.cn/cb22802e18cd4eb2b6c5f7fdb8bac82b.png" alt="">
          </div>
            <div class="blog_extension_card_cont">
              <div class="blog_extension_card_cont_l">
                <span class="text">孤寒者</span>
                <div class="blog_extension_card_cont_r">
                  <img class="weixin" src="https://g.csdnimg.cn/extension-box/1.1.6/image/weixin.png" alt="">
                  <span>微信公众号</span>
                  <img class="go" src="https://g.csdnimg.cn/extension-box/1.1.6/image/ic_move.png" alt="">
                </div>
              </div>
              <span class="style">持续输出python爬虫领域以及web方向干货文！</span>
            </div>
          </div></div></div>
    </article>
  <script>
    $(function() {
      setTimeout(function () {
        var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
        if (mathcodeList.length > 0) {
          for (let i = 0; i < mathcodeList.length; i++) {
            if (mathcodeList[i].complete) {
              if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
                var alt = mathcodeList[i].alt;
                alt = '\\(' + alt + '\\)';
                var curSpan = $('<span class="img-codecogs"></span>');
                curSpan.text(alt);
                $(mathcodeList[i]).before(curSpan);
                $(mathcodeList[i]).remove();
              }
            } else {
              mathcodeList[i].onerror = function() {
                var alt = mathcodeList[i].alt;
                alt = '\\(' + alt + '\\)';
                var curSpan = $('<span class="img-codecogs"></span>');
                curSpan.text(alt);
                $(mathcodeList[i]).before(curSpan);
                $(mathcodeList[i]).remove();
              };
            }
          }
          MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
        }
      }, 500)
    });
  </script>
</div>
</body>
</html>